#ifndef __H_GAME_H__
#define __H_GAME_H__

//#include <deque>
#include <vector>
#include <map>
#include "ICC_Timer_Handler.h"
#include "RedisClient.h"
#include "GameBase.h"
#include "DbPart.h"
#include "CClient.h"
#include "def.h"
#include "HostBase.h"
#include "queue"

using namespace std;

struct stPlayerHandler
{
	CClientHandler* m_phandler;
	stPlayerHandler():m_phandler(NULL)
	{
	}

	stPlayerHandler(const stPlayerHandler& other)
	{
		m_phandler = other.m_phandler;
	}

	stPlayerHandler& operator = (const stPlayerHandler& other)
	{
		if (this == &other)
			return *this;

		m_phandler = other.m_phandler;

		return *this;
	}
};

class CHost;
class CGameServer :public BYTimerOject
{
public:
	CGameServer();
	virtual ~CGameServer();
public:
	virtual bool InitGame();
	virtual int OnBYTimeOut(int Timerid);
	int ProcessPacket(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcEventBackConn(int ev_id, SpiderEvent* pEvent);
	int ProcEventBackClose(int ev_id, SpiderEvent* pEvent);
	int ProcEventClientClose(int ev_id, SpiderEvent* pEvent);
	int ProcEventGameInit(int ev_id, SpiderEvent* pEvent);
	int ProcEventPublishService(int ev_id, SpiderEvent* pEvent);
	int ProcConnCheck(int ev_id, SpiderEvent* pEvent);
private:
    int ProcPlayerPack(SocketHandler* pHandler, NETInputPacket* inputPkg);
    int ProcSrvPack(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcOnlineSrvPack(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcPlayerAccount(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcPlayerRoomAlloc(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcLoginRsp(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcRoomAllocReg(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcUserBanPush(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcKick(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int srvtypeModSend(unsigned short stype, unsigned int mod, CCPacketBase* pack);
	int srvtypeRandSend(unsigned short stype, NETInputPacket* pack);
	int sendToPlayer(SocketHandler* pHandler, NETInputPacket* pack);
	int sendToSrv(SocketHandler* pHandler, NETInputPacket* pack);
	int sendToOneSrv(unsigned short stype, unsigned int sid, NETInputPacket* pack);
	void ProcHeartBeat(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int synToOnlineOn(unsigned int uid);
	int synToOnlineOff(unsigned int uid);
	int synOnline(CClient* client);
	void procSynOnline(const unsigned int sid, CClient* client = NULL);
	int procHeartBeatSyn(void);
	void resetSynCount(SocketHandler* pHandler);
	void kick(const unsigned int uid, const int reason, const bool syn = false);
	//unsigned int genTmpUin(void);
	//map<unsigned short, map<unsigned int, CClient*> > m_node;
	//map<unsigned short, vector<unsigned int> > m_nodeId;
	//map<unsigned int, CClientHandler*> m_unloginPlayer;
	//map<unsigned int, CClientHandler*> m_loginPlayer;
	map<unsigned int, stPlayerHandler> m_unloginPlayer;
	map<unsigned int, stPlayerHandler> m_loginPlayer;
	map<unsigned int, stPlayerHandler> m_kickedPlayer;
	queue<unsigned int> m_synOnlineSid;
	bool m_timerflag;

	map<unsigned int, set<unsigned int> > m_roomAlloc;
	map<unsigned int, unsigned int> m_sidGameid;
public:
	CHost* m_pHost;
};


#endif


